package com.gongpingjialoan.activity.sell;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.fb.MyPushIntentService;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.SellPrivateDetail;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.StepRecord;
import com.google.gson.Gson;

import org.json.JSONObject;

/**
 * Created by Administrator on 2016/10/11.
 */
public class SellPrivateCarDetailActivity extends BaseActivity implements NetDataJson.OnStringListener, View.OnClickListener {

    private NetDataJson mNetDataJson, deleteNetDataJson;

    private String fromType, id;

    private TextView deleteTextView, modifyTextView;

    private SellPrivateDetail mSellPrivateDetail;

    public OffSellPrivateCarDetailFragment mOffSellPrivateCarDetailFragment;

    public OnSellPrivateCarDetailFragment mOnSellPrivateCarDetailFragment;

    public boolean isModify = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "my_sellRecord_personalDetail";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sell_car);
        setTitle("卖车记录");
        fromType = getIntent().getStringExtra("type");
        id = getIntent().getStringExtra("typevalue");
        String job_id = getIntent().getStringExtra("job_id");
        String source = getIntent().getStringExtra("source");
        if (MyPushIntentService.MYCAR.equals(fromType)) {
            StepRecord.recordStep(this, "pushnotification", "{type:" + fromType + ",source:" + source + ",job_id:" + job_id + "}");
        }
        deleteTextView = (TextView) findViewById(R.id.right);
        deleteTextView.setText("删除");
        deleteTextView.setTextColor(0xff333333);
        deleteTextView.setOnClickListener(this);

        modifyTextView = (TextView) findViewById(R.id.right_title);
        modifyTextView.setText("修改");
        modifyTextView.setTextColor(0xff333333);
        modifyTextView.setOnClickListener(this);

        mNetDataJson = new NetDataJson(this, 1);
        mNetDataJson.setUrl(API.getPrivate_detail);
        mNetDataJson.addParam("id", id);
        mNetDataJson.request("get");
        showProgressDialog("加载中...");
    }


    @Override
    public void onBackPressed() {
        //表示从推送而来
        if (MyPushIntentService.SELLPRIVATECAR.equals(fromType)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            if (isModify && mSellPrivateDetail != null && mSellPrivateDetail.getData() != null && "S".equals(mSellPrivateDetail.getData().getStatus())) {
                setResult(Activity.RESULT_OK);
                finish();
            } else {
                super.onBackPressed();
            }

        }
    }

    @Override
    public void initTitleBar() {
        View backV = findViewById(R.id.title_back);
        if (backV != null) {
            backV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    onBackPressed();
                }
            });
        }
    }

    @Override
    public void onDataJsonError(String errorMessage) {
        hidenProgressDialog();
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDataJsonUpdate(String json) {
        hidenProgressDialog();
        Gson gson = new Gson();
        mSellPrivateDetail = gson.fromJson(json, SellPrivateDetail.class);
        if ("success".equals(mSellPrivateDetail.getStatus())) {
            //initData();
            deleteTextView.setVisibility(View.VISIBLE);
            if (!"S".equals(mSellPrivateDetail.getData().getStatus())) {
                mOffSellPrivateCarDetailFragment = new OffSellPrivateCarDetailFragment(mSellPrivateDetail.getData());
                getSupportFragmentManager().beginTransaction().replace(R.id.content, mOffSellPrivateCarDetailFragment).commit();
            } else {
                modifyTextView.setVisibility(View.VISIBLE);
                mOnSellPrivateCarDetailFragment = new OnSellPrivateCarDetailFragment(mSellPrivateDetail.getData());
                getSupportFragmentManager().beginTransaction().replace(R.id.content, mOnSellPrivateCarDetailFragment).commit();
            }
        } else {
            Toast.makeText(this, mSellPrivateDetail.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        if (v == deleteTextView) {
            AlertDialog.Builder ab = new AlertDialog.Builder(this);
            ab.setTitle("提示");
            ab.setMessage("确定要删除吗?");
            ab.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    deleteHistory();
                }
            });
            ab.create().show();
        } else if (v == modifyTextView) {
            if (mSellPrivateDetail != null)
                mOffSellPrivateCarDetailFragment = new OffSellPrivateCarDetailFragment(mSellPrivateDetail.getData());
            getSupportFragmentManager().beginTransaction().replace(R.id.content, mOffSellPrivateCarDetailFragment).commit();
            modifyTextView.setVisibility(View.GONE);
        }
    }


    private void deleteHistory() {
        if (deleteNetDataJson == null) {
            deleteNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
                @Override
                public void onDataJsonError(String errorMessage) {
                    hidenProgressDialog();
                    Toast.makeText(SellPrivateCarDetailActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onDataJsonUpdate(JSONObject json) {
                    hidenProgressDialog();
                    setResult(Activity.RESULT_OK);
                    finish();
                    Toast.makeText(SellPrivateCarDetailActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
            });
        }
        deleteNetDataJson.setUrl(API.delete_personal_car);
        deleteNetDataJson.addParam("id", id);
        deleteNetDataJson.request("post");
        showProgressDialog("加载中...");
    }
}
